package com.miyake.dsp.generator;

import com.miyake.dsp.ComplexArray;
import com.miyake.dsp.Processor;

public class NoiseGenerator implements Processor {
	
	private ComplexArray source;
	private NoiseSetting setting;
	
	
	
	public NoiseGenerator(NoiseSetting setting) {
		super();
		this.setting = setting;
	}

	@Override
	public void setSource(ComplexArray source) {
		this.source = source;
	}

	@Override
	public ComplexArray getProcessed() {
		ComplexArray ret;
		
		if (source != null) {
			if (source.getImagPart() != null) {
				ret = new ComplexArray(source.getRealPart().clone(), source.getImagPart().clone());
			}
			else {
				ret = new ComplexArray(source.getRealPart().clone());
			}
		}
		else {
			ret = new ComplexArray(new double[1024]);
		}
		
		for (int i = 0; i < ret.getRealPart().length; i++) {
			ret.setRealPart(i, ret.getRealPart()[i] + (Math.random() - 0.5) * setting.getMagnitude());
		}
		return ret;
	}

	public NoiseSetting getSetting() {
		return setting;
	}

	public void setSetting(NoiseSetting setting) {
		this.setting = setting;
	}

	@Override
	public ComplexArray getSource() {
		return this.source;
	}

	
}
